package erfgame.core.world.terrain.color;


import erfgame.core.util.ColorUtils;
import erfgame.core.world.World;
import erfgame.core.world.terrain.TerrainColorSource;

public class UnknownTerrainColorSource implements TerrainColorSource {

	private static final int[][] UNKNOWN_COLORS = {{0xFF333355, 0xFF779977},{0xFF000022, 0xFF220000},{0xFF553333, 0xFF555533}};
	
	
	public static final int[][][] generateColorMap( int gridWidth, int depth ) {
		int[][][] colorMap = new int[gridWidth*2][gridWidth*2][depth];
		int zSectionLength = depth / (UNKNOWN_COLORS.length-1);
		for( int x=gridWidth*2; x>0; ) {
			x--;
			for( int y=gridWidth*2; y>0; ) {
				y--;
				for( int z=depth; z>0; ) {
					z--;
					int gridIndex = ((x/gridWidth) + (y/gridWidth) + (z/gridWidth))%2;
					int sectionIndex = z / zSectionLength;
					int sectionMultiplier = z % zSectionLength;
					int fromColor = UNKNOWN_COLORS[sectionIndex][gridIndex];
					int toColor = UNKNOWN_COLORS[sectionIndex+1][gridIndex];
					
					colorMap[x][y][z] = ColorUtils.multiplyColorUsingDiv(fromColor, toColor, sectionMultiplier, zSectionLength);
				}
			}
		}
		return colorMap;
	}
	
	private int[][][] colorMap;
	private int xMask;
	private int yMask;
	
	public UnknownTerrainColorSource( World world ) {
		this( generateColorMap( world.getGridDiagonal()/4, world.getWorldDepth() ), world.getGridDiagonalMask()>>1, world.getGridDiagonalMask()>>1 );
	}
	
	public UnknownTerrainColorSource( int[][][] colorMap, int xMask, int yMask ) {
		this.colorMap = colorMap;
		this.xMask = xMask;
		this.yMask = yMask;
	}

	public int getColor(int x, int y, int z, int mult, int div, int dCeiling) {
		int mapX = x & xMask;
		int mapY = y & yMask;
		int mapZ = z;
		return colorMap[mapX][mapY][mapZ];
	}

}
